global input "Q:\dc1prhcmsas01\PU2\data - sas"
global temp "Q:\dc1prhcmsas01\PU2\temp_stata_wr"
global log "Q:\dc1prhcmsas01\PU2\Log - Stata WR"
global output "Q:\dc1prhcmsas01\PU2\data_stata_wr"
global graphs "Q:\dc1prhcmsas01\PU2\graphs_stata_wr"
global tables "Q:\dc1prhcmsas01\PU2\tables_stata_wr"

cd "Q:\dc1prhcmsas01\PU2\Code - Stata WR"
set scheme plotplain
graph set window fontface "Times New Roman"

capture log close
log using "$log/19_firm_characteristics", replace


****************************************
*Define share of firm treated 
****************************************
	use if yr_month==mdy(4,1,2016) using "$output\unbalanced_panel_treat.dta", clear
	
	*Share treated
		gen treat_sal=salaried0==1 & base0>=455 & base0<=913
		
	*Census region		
	gen west= state=="AK" | state=="WA" | state=="CA" | state=="HI" | ///
			state=="ID" | state=="MT" | state=="WY" | state=="NV" | state=="UT" | state=="CO" | state=="AZ" | state=="NM"
			
	gen midwest= state=="ND" | state=="SD" | state=="NE" | state=="KS" | state=="MN" | state=="IA" | state=="MO" | /// 
		state=="WI" | state=="MI" | state=="IL" | state=="IN" | state=="OH"
		
	gen south= state=="TX" | state=="OK" | state=="AR" | state=="LA" | ///
		state=="KY" | state=="TN" | state=="MS" | state=="AL" | ///
		state=="WV" | state=="MD" | state=="DC" | state=="DE" | state=="VA" | state=="NC" | state=="SC" | state=="GA" | state=="FL"
		
	gen northeast= state=="PA" | state=="NY" | state=="NJ" | ///
		state=="ME" | state=="VT" | state=="NH" | state=="MA" | state=="CT" | state=="RI"
			
	*Industry 
	gen naics2 = int(naics/10000)
	gen agg = naics2<=21
	gen construction=naics2>=22 & naics2<=23
	gen manu= naics2>=31 & naics2<=33
	gen retail = naics2>=42 & naics2<=45 
	gen transport = naics2>=48 & naics2<=49
	gen professional = naics2>=51 & naics2<=56
	gen education = naics2==61 
	replace health = naics2==62 
	gen rest=naics2>=71 & naics2<=72
	gen other = naics2==81
	gen public = naics2==92
			
	*Collapse by firm 
	collapse (mean) treat_sal male salaried base west midwest south northeast agg construction manu retail transport professional education health rest other public (count) n=base, by(client_id)
	egen p33 = pctile(treat_sal), p(33) 
	egen p66 = pctile(treat_sal), p(66)
	gen tercile=1 if treat_sal<=p33
	replace tercile=2 if treat_sal>p33 & treat_sal<=p66 
	replace tercile=3 if treat_sal>p66
	
	*Collapse by tercile
	collapse (mean) p33 p66 treat_sal n base male salaried ///
		west midwest south northeast ///
		agg construction manu retail transport professional education health rest other public (count) obs_firm=n, by(tercile)
	
	
	
*******************************************************************************************
*********************************Summary Stats Table***************************************
*******************************************************************************************
forvalues i=1/3{
	foreach var in p33 p66 treat_sal n base male salaried ///
		west midwest south northeast ///
		agg construction manu retail transport professional education health rest other public ///
		obs_firm {
		sum `var' if tercile==`i'
		global `var'`i'=round(r(mean), 0.01)
	}
}


capture erase "$tables/table_appendix_sum_stats.tex"
	file open fh3 using "$tables/table_appendix_sum_stats.tex", write replace

	file write fh3 ///
		"\begin{tabular}{@{\hskip\tabcolsep\extracolsep\fill} l cc cc cc cc}" _n ///
		"\toprule" _n ///
		"& \multicolumn{6}{c}{Share Treated} \\" _n ///
		"\cmidrule(lr){2-7} " _n ///
		" && Treat \leq $p333 && $p333<Treat \leq $p663 && Treat>$p663 \\" _n ///
		"\midrule" _n ///
		"Fraction Treated && $treat_sal1 && $treat_sal2 && $treat_sal3 \\" _n ///
		"Number of Workers && $n1 && $n2 && $n3 \\" _n ///
		"Avg. Weekly Base Pay && $base1 && $base2 && $base3 \\" _n ///
		"Share Male && $male1 && $male2 && $male3 \\" _n ///
		"Share Salaried && $salaried1 && $salaried2 && $salaried3 \\" _n ///
		"[1em] " _n ///
		"West && $west1 && $west2 && $west3 \\" _n ///
		"Midwest && $midwest1 && $midwest2 && $midwest3 \\" _n ///
		"South && $south1 && $south2 && $south3 \\" _n ///
		"Northeast && $northeast1 && $northeast2 && $northeast3 \\" _n ///
		"[1em] " _n ///
		"Agriculture \& Mining && $agg1 && $agg2 && $agg3 \\" _n ///
		"Construction && $construction1 && $construction2 && $construction3 \\" _n ///
		"Manufacturing && $manu1 && $manu2 && $manu3 \\" _n ///
		"Retail and Wholesale && $retail1 && $retail2 && $retail3 \\" _n ///
		"Transportation && $transport1 && $transport2 && $transport3 \\" _n ///
		"Professional Services && $professional1 && $professional2 && $professional3 \\" _n ///
		"Education && $education1 && $education2 && $education3 \\" _n ///
		"Health && $health1 && $health2 && $health3 \\" _n ///
		"Restaurants && $rest1 && $rest2 && $rest3 \\" _n ///
		"Public Services && $public1 && $public2 && $public3 \\" _n ///
		"Other && $other1 && $other2 && $other3 \\" _n ///
		"\midrule" _n ///
		"No. Firms && $obs_firm1 && $obs_firm2 && $obs_firm3 \\" _n ///
		"\bottomrule" _n ///
		"\end{tabular}" _n
		
	file close fh3 
	macro drop fh3 


log close
